var Login = Login || {};
(function() {
	var $ = jQuery;
	this.show_div = function () {
		var docH = $(document).height();
		$("#login_cover_div").css("display","block").height(docH);
		$("#login_div").css("display","block");
	},
	this.hidden_div = function () {
		$("#login_cover_div").css("display","none");
		$("#login_div").css("display","none");
	},
	this.login = function(username, password, rem) {
		var url = wwwroot+"user.php";
		var data = {
			"username":username,
			"password":password,
			"rem":rem?1:0,
			"act":"signin"
		};
		$.ajax({
			type:"POST",
			url:url,
			cache:false,
			data: httpBuildQuery(data), 
			dataType: "json",
			success: function(result) {
				if(result.error) {
					alert(result.content);
					return false;
				}
				Login.hidden_div();
				location.reload();
			}
		});
	},
	this.isLogined = function(callback) {
		var url = wwwroot+"user.php";
		var data = {
			"act":"check_status"
		};
		$.ajax({
			type:"POST",
			url:url,
			cache:false,
			data: httpBuildQuery(data), 
			dataType: "json",
			success: callback
		});		
	},
	this.logout = function(callback) {
		if (!callback) {
			callback = function() {
				location.href = wwwroot;
			}
		}
		var url = wwwroot+"user.php";
		var data = {
			"act":"aj_logout"
		};
		$.ajax({
			type:"POST",
			url:url,
			cache:false,
			data: httpBuildQuery(data), 
			dataType: "json",
			success: callback
		});	
	}
}).call(Login);

var Reg = Reg || {};
(function() {
	var $ = jQuery;
	this.show_div = function () {
		var docH = $(document).height();
		$("#reg_cover_div").css("display","block").height(docH);
		$("#reg_div").css("display","block");
	},
	this.hidden_div = function () {
		$("#reg_cover_div").css("display","none");
		$("#reg_div").css("display","none");
	},
	this.reg = function(f){
		var username=$('#username'), password=$('#password'), password2=$('#password2'),email=username;
		var vusername=username.val().trim(),vpassword=password.val(),vpassword2=password2.val();
		if (vusername=='') {
			alert(email_empty);
			return false;
		}
		if (!/^([a-z0-9+_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,6}$/i.test(vusername)) {
			alert(email_invalid);
			return false;
		}
		
		if (vpassword === '') {
			alert(password_empty);
			return false;
		}
		if (vpassword.length < 6) {
			alert(password_shorter);
			return false;
		}
		if (vpassword !== vpassword2) {
			alert(confirm_password_invalid);
			return false;
		}
		
		var data = {
			'username' : vusername,
			'password' : vpassword,
			'email' : vusername,
			'isajax' : 1,
			'agreement' : 1,
			'act' : 'act_register'
		};
		
		$.post(wwwroot+"user.php", data, function(msg){
			if (msg.msg.indexOf(vusername) == -1) {
				alert(msg.msg);
			} else {
				location.reload();
			}
		}, 'json');
		return false;
	}
}).call(Reg);


function checkSearchForm(f) {
    if(jQuery(f).find("input")[0].value) {
        return true;
    } else {
        alert("请输入搜索关键词！");
        return false;
    }
}
